package com.rural.bus.driver;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.rural.bus.driver.config.MyPreferenceManager;
import com.rural.bus.driver.config.PreferenceKey;
import com.rural.bus.driver.model.DriverInfo;
import com.rural.bus.driver.net.MyHttpUtil;
import com.rural.bus.driver.net.OnNetCallback;
import com.rural.bus.driver.net.Url;
import com.rural.bus.driver.utils.CommonTools;
import com.rural.bus.driver.utils.ProgressUtil;
import com.rural.bus.driver.view.CircleImageView;
import com.squareup.picasso.Picasso;

public class MyAccountActivity extends BaseActivity {

	private CircleImageView userimg;
	private TextView username, tv_car_number, tv_car_type, tv_car_color, tv_my_phone, tv_my_account_balance;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_my_account);

		initView();
		setListeners();
		initData();
	}

	protected void setListeners() {

	}

	@Override
	protected void initView() {
		mBtMiddle.setText("我的账户");
		mBtMiddle.setVisibility(View.VISIBLE);

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);
		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MyAccountActivity.this.finish();
			}
		});

		userimg = (CircleImageView) findViewById(R.id.avatar_pic);
		username = (TextView) findViewById(R.id.user_name);

		tv_car_number = (TextView) findViewById(R.id.tv_car_number);
		tv_car_type = (TextView) findViewById(R.id.tv_car_type);
		tv_car_color = (TextView) findViewById(R.id.tv_car_color);
		tv_my_phone = (TextView) findViewById(R.id.tv_my_phone);
		tv_my_account_balance = (TextView) findViewById(R.id.tv_my_account_balance);

	}

	@Override
	protected void initData() {

		getDriverInfo();

		getDriverBalance();

	}

	private void getDriverInfo() {
		MyHttpUtil http = new MyHttpUtil();
		http.addBodyParm("driverId", "" + MyApplication.driverInfo.drivId);
		ProgressUtil.showProgress(this, "");
		http.send(Url.get_driver_info, new OnNetCallback() {

			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				// {"msg":"ok","status":"0","result":{"createTime":1453536034000,"phone":"13522761340","sex":1,"status":0,"score":500,"remarks":"","carSeat":4,"photo":"9303c3a72be943a8bb3a7b48d626ec48.jpg","password":"96E79218965EB72C92A549DD5A330112","carType":0,"cardPhoto":null,"drivId":1,"name":"陈陈2","carName":"宝马","drivPhoto":"","cardNum":"湘A7897"}}
				try {
					if (object.get("status").equals("0")) {
						MyPreferenceManager.instance().putSharedInfoOfString(PreferenceKey.USER_INFO, object.optString("result", ""));
						MyApplication.driverInfo = new Gson().fromJson(object.optString("result", ""), new TypeToken<DriverInfo>() {
						}.getType());
						fillViewWithData();
					} else {
						toast(R.string.data_error);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
			}
		});

	}

	private void fillViewWithData() {
		if (!CommonTools.isEmpty(MyApplication.driverInfo.name)) {
			username.setText(MyApplication.driverInfo.name);
		}

		if (!CommonTools.isEmpty(MyApplication.driverInfo.drivPhoto)) {
			Picasso.with(this).load(MyApplication.driverInfo.drivPhoto).error(R.drawable.default_headimg).into(userimg);
		}

		if (!CommonTools.isEmpty(MyApplication.driverInfo.cardNum)) {
			tv_car_number.setText(MyApplication.driverInfo.cardNum);
		}

		if (!CommonTools.isEmpty(MyApplication.driverInfo.name)) {
			tv_car_color.setText(MyApplication.driverInfo.name);
		}

		switch (MyApplication.driverInfo.carType) {
		case 1:
			tv_car_type.setText("舒服车型");
			break;
		case 2:
			tv_car_type.setText("商务车型");
			break;
		case 3:
			tv_car_type.setText("豪华车型");
			break;
		default:
			tv_car_type.setText("经济车型");
			break;
		}

		if (!CommonTools.isEmpty(MyApplication.driverInfo.phone)) {
			tv_my_phone.setText(MyApplication.driverInfo.phone);
		}
	}

	private void getDriverBalance() {

		MyHttpUtil http = new MyHttpUtil();
		http.addBodyParm("haveId", "" + MyApplication.driverInfo.drivId);
		http.addBodyParm("type", "1");
		http.send(Url.get_balance, new OnNetCallback() {

			@Override
			public void success(JSONObject object) {
				// {"msg":"ok","status":"0","result":833.3}
				try {
					if (object.get("status").equals("0")) {
						double balance = object.optDouble("result");
						tv_my_account_balance.setText(balance + "元");
					} else {
						toast(R.string.data_error);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void fail(String error) {
				toast("网络错误，余额获取失败");
			}
		});

	}

}
